package com.wzxy.spider.tbw;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wzxy.common.Const;
import com.wzxy.filter.EmojFilter;
import com.wzxy.spider.tbw.entity.TbwNews;
import com.wzxy.spider.tbw.service.ITbwService;
import com.wzxy.util.JsoupUtils;
import com.wzxy.util.MyTimeUtils;

@Service("tbwSpider")
public class TbwSpider {
	
	@Autowired
	private ITbwService tbwService;
	
	public void startSpider(){
		try {
			getzuo();
			getxiazuo();
			getxiayou();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ReflectiveOperationException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void getzuo() throws SQLException, ReflectiveOperationException, Exception{
		List<TbwNews> list = new ArrayList<TbwNews>();
		TbwNews tbwNews = null;
		Document doc = JsoupUtils.getDoc(Const.TBW_URL);

		Elements es = doc.select(".rank-list>li>a");
		for (Element element : es) {
			if(removeRepeat(element.text())){
				tbwNews =new TbwNews();
				tbwNews.setTitle(element.text());
				tbwNews.setUrl(Const.TBW_URL+element.attr("href"));
				Document doc2 = JsoupUtils.getDoc(Const.TBW_URL+element.attr("href"));
				String html = doc2.html();			
				tbwNews.setHtml(EmojFilter.filterEmoji(html));
				tbwNews.setStatus(com.wzxy.common.Const.PREPARE);
				tbwNews.setSpiderTime(MyTimeUtils.getHasHourString());
				list.add(tbwNews);
			}
		}

		/** 保存到数据库 */
		tbwService.saveAll(list);
	}
	
	public void getxiayou() throws SQLException, ReflectiveOperationException, Exception{
		List<TbwNews> list = new ArrayList<TbwNews>();
		TbwNews tbwNews = null;
		Document doc = JsoupUtils.getDoc(Const.TBW_URL);

		Elements es = doc.select(".infoslist>ul>li>a");
		for (Element element : es) {
			if(removeRepeat(element.text())){
				tbwNews =new TbwNews();
				tbwNews.setTitle(element.text());
				tbwNews.setUrl(Const.TBW_URL+element.attr("href"));
				Document doc2 = JsoupUtils.getDoc(Const.TBW_URL+element.attr("href"));
				tbwNews.setHtml(doc2.html());
				tbwNews.setStatus(com.wzxy.common.Const.PREPARE);
				tbwNews.setSpiderTime(MyTimeUtils.getHasHourString());
				list.add(tbwNews);		
			}
		}
		
		/** 保存到数据库 */
		tbwService.saveAll(list);
	}
	
	public void getxiazuo() throws SQLException, ReflectiveOperationException, Exception{
		List<TbwNews> list = new ArrayList<TbwNews>();
		TbwNews tbwNews = null;
		Document doc = JsoupUtils.getDoc(Const.TBW_URL);

		Elements es = doc.select(".tuilist>ul>li>a");
		for (Element element : es) {
			if(removeRepeat(element.text())){
				tbwNews =new TbwNews();
				tbwNews.setTitle(element.text());
				tbwNews.setUrl(Const.TBW_URL+element.attr("href"));
				Document doc2 = JsoupUtils.getDoc(Const.TBW_URL+element.attr("href"));
				tbwNews.setHtml(doc2.html());
				tbwNews.setStatus(com.wzxy.common.Const.PREPARE);
				tbwNews.setSpiderTime(MyTimeUtils.getHasHourString());
				list.add(tbwNews);
			}
		}
		
		/** 保存到数据库 */
		tbwService.saveAll(list);
	}
	
	public boolean removeRepeat(String title){
		boolean isRepeat = true;
		try {
			List<TbwNews> list = tbwService.findAll();
			if (list.size() > 0) {
				for (TbwNews tbwNews : list) {
					if (title.equals(tbwNews.getTitle())) {
						isRepeat = false;
						break;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isRepeat;
	}
	
	
}
